package com.digitalart.site.section {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.getClassByAlias;
	import flash.net.registerClassAlias;
	import com.digitalart.site.section.subsectiontype.SubSectionType;
	/**
	 * @author johan.chaves
	 */
	 
	public class SectionFactory extends EventDispatcher{
		
		public static const ON_SECTION_READY : String = "onsectionready";
		public static const SUBSECTION_TYPE : String = "subsection";
		
		private static var _instance : SectionFactory;
		
		private var _section : Section;		

		public function SectionFactory(singleton : SingletonEnforcer){
			registerSections();
		}
		
		private function registerSections() : void {
			
			registerClassAlias(SUBSECTION_TYPE, SubSectionType);			
		}
		
		public static function getInstance():SectionFactory{
			if(_instance == null) {
				_instance = new SectionFactory(new SingletonEnforcer());
			}
			return _instance; 
		}
		
		public function prepareSection(id : String, type : String) : void {
			
			var targetClass:Class = getClassByAlias(type) ;				
			section = new targetClass();
			section.addEventListener(Section.ON_LOAD_COMPLETE,onSectionLoadComplete);
			section.id = id;
		}
		
		public function unloadSection() : void {
			
			section = null;
		}
		
		public function createSection():void{
			
			section.load();
		}
		
		private function onSectionLoadComplete(event : Event) : void {
			dispatchEvent(new Event(ON_SECTION_READY));
		}
		
		public function get section() : Section {
			return _section;
		}
		
		public function set section(section : Section) : void {
			_section = section;
		}
	}	
}
class SingletonEnforcer{}